package com.burst.reptile.modular.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.burst.reptile.common.constant.Constants;
import com.burst.reptile.common.utils.ServletUtils;
import com.burst.reptile.framework.security.LoginBody;
import com.burst.reptile.framework.security.LoginUser;
import com.burst.reptile.framework.security.service.SysLoginService;
import com.burst.reptile.framework.security.service.SysPermissionService;
import com.burst.reptile.framework.security.service.TokenService;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.system.entity.SysMenu;
import com.burst.reptile.modular.system.entity.SysUser;
import com.burst.reptile.modular.system.service.ISysMenuService;

import lombok.RequiredArgsConstructor;

/**
 * 登录验证
 * @author wujin
 * @date: 2020年5月22日 下午2:10:02
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysLoginController {
	
	private final SysLoginService loginService;

	private final ISysMenuService menuService;

	private final SysPermissionService permissionService;

	private final TokenService tokenService;

	/**
	 * 登录方法
	 * @param username 用户名
	 * @param password 密码
	 * @param captcha  验证码
	 * @param uuid     唯一标识
	 * @return 结果
	 */
	@PostMapping("/login")
	public Result<Object> login(@RequestBody LoginBody loginBody) {
		// 生成令牌
		String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
				loginBody.getUuid());
		Map<String, String> map = new HashMap<>();
		map.put(Constants.TOKEN, token);
		return Result.success(map);
	}

	/**
	 * 获取用户信息
	 * 
	 * @return 用户信息
	 */
	@GetMapping("/getInfo")
	public Result<Object> getInfo() {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		// 角色集合
		Set<String> roles = permissionService.getRolePermission(user);
		// 权限集合
		Set<String> permissions = permissionService.getMenuPermission(user);
		
		Map<String, Object> map = new HashMap<>();
		map.put("user", user);
		map.put("roles", roles);
		map.put("permissions", permissions);
		return Result.success(map);
	}

	/**
	 * 获取路由信息
	 * 
	 * @return 路由信息
	 */
	@GetMapping("/getRouters")
	public Result<Object> getRouters() {
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		// 用户信息
		SysUser user = loginUser.getUser();
		List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
		return Result.success(menuService.buildMenus(menus));
	}
	
}
